//路由参数练习

//1.导入express包
const express = require('express');

//2.创建express对象
const expressmodel = express();

//3.获取json文件中的数据  两种方法  1、require导入  2、fs读取
const {singers} = require("./singers.json");


//创建一个路由
expressmodel.get("/:id.html",(request,response)=>{

    //获取路由参数 :id
    let {id} = request.params;

    //根据路由参数 找到json中的匹配数据
    let singer = singers.find(item=>{
        return item.id == Number(id)
    });

    if(!singer){
        response.statusCode = 404;
        response.statusCode = 404;
        response.statusCode = 404;
        response.end("<h1>404 Not found</h1>");
    }

    console.log(singer);
    response.end(`<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <h1>${singer.singer_name}</h1><h2>${singer.singer_id}</h2>
    </body>
    </html>`);  
})

expressmodel.listen(3000,()=>{
    console.log("服务已经启动，端口3000正在监听中...");
});